﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SumiaAccountBook.SubControls
{
    /// <summary>
    /// ucProject.xaml 的交互逻辑
    /// </summary>
    public partial class ucProject : UserControl
    {
        const int PageSize = 20;
        private int page_count = 1;
        public List<Biz.ProjectObj> Projects { set; get; }

        public ucProject()
        {
            InitializeComponent();

            this.dpStart.Text = DateTime.Now.ToShortDateString();

            this.cbStatus.ItemsSource = Biz.KVConstHelper.GetInstance().GetAllValue < Biz.KVProjectStatus>();
            this.cbStatus.SelectedIndex = 1;
            
            bindData();
            this.DataContext = this;

        }        

        private void bindData()
        {
            int t1 = 0;
            this.Projects = Biz.GetList.GetListProject(page_count, PageSize, ref t1);
            this.dg_project.ItemsSource = this.Projects;
            this.dp_project.TotalCount = t1;
        }

        private void btn_project_add_Click(object sender, RoutedEventArgs e)
        {            
            string name = this.tb_projec_name.Text.Trim();
            if(string.IsNullOrEmpty(name))
            {
                MessageBox.Show("名称不能为空！");
                return;
            }
            string tmpContractSum=this.tb_ContractSum.Text.Trim();
            int contractSum=0;
            int.TryParse(tmpContractSum,out contractSum);
            string tmpSum=this.tb_Sum.Text.Trim();
            int sum=0;
            int.TryParse(tmpSum,out sum);
            string statusName = this.cbStatus.SelectedValue as string;
            int status = Biz.KVConstHelper.GetInstance().GetKey < Biz.KVProjectStatus>(statusName);
            DateTime start = DateTime.MinValue;
            string tmpStart = this.dpStart.Text.Trim();
            if(!string.IsNullOrEmpty(tmpStart))
            {
                start = Convert.ToDateTime(tmpStart);
            }
            DateTime end = DateTime.MinValue;
            string tmpEnd = this.dpEnd.Text.Trim();
            if (!string.IsNullOrEmpty(tmpEnd))
            {
                end = Convert.ToDateTime(tmpEnd);
            }
            DataAccess.Entity.Project project =
                new DataAccess.Entity.Project { Name = name, StartDate = start, ContractSum = contractSum, EndDate = end, Sum = sum, Status = status, Memo = this.tb_Memo.Text.Trim() };
              
            try
            {
                //DataAccess.DaCommonADO.InsertProject(project);
                DataAccess.DaCommon<DataAccess.Entity.Project>.Add(project);
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            bindData();
        }

        private void dp_project_PageChanged(object sender, CubicFamily.StockTool.ComonControl.PageChangedEventArgs e)
        {
            this.page_count = e.CurrentPageIndex;
            bindData();
        }

        private void dg_project_remove_Click(object sender, RoutedEventArgs e)
        {
            var original = e.OriginalSource as Button;
            var key = original.CommandParameter;
            int Id = Convert.ToInt32(key);
            MessageBoxResult confirmToDel = MessageBox.Show("确认要删除所选行吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (confirmToDel == MessageBoxResult.Yes)
            {
                try
                {
                    DataAccess.DaCommon<DataAccess.Entity.Project>.Delete(Id);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                
                bindData();
            }
        }

    }
}
